<template>
  <div>
    <el-form ref="form" :model="form" inline label-width="80px">
      <el-form-item label="校区" style="font-weight:bold;">
        <el-select v-model="form.campusId" placeholder="请选择校区" style="width:150px;" clearable>
          <el-option v-for="item in CampusList" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="楼宇" style="font-weight:bold;">
        <el-select v-model="form.buildId" placeholder="请选择楼宇" style="width:150px">
          <el-option v-for="item in buildName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="姓名" style="font-weight:bold;">
        <el-input v-model="form.userName" style="width:150px" placeholder="请输入姓名"></el-input>
      </el-form-item>
      <el-form-item label="学号" style="font-weight:bold;">
        <el-input v-model="form.userNum" style="width:150px" placeholder="请输入学号"></el-input>
      </el-form-item>
      <el-form-item label="年级" style="font-weight:bold;">
        <el-input v-model="form.grade" style="width:150px" placeholder="(如2021级)"></el-input>
      </el-form-item>
      <el-form-item label="学生类型" style="font-weight:bold;">
        <el-select v-model="form.type" placeholder="请选择学生类型" style="width:150px;" clearable>
          <el-option label="本科生" value="0"></el-option>
          <el-option label="硕士生" value="1"></el-option>
          <el-option label="博士生" value="2"></el-option>
          <el-option label="留学生" value="3"></el-option>
          <el-option label="交换生" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="院系" style="font-weight:bold;">
        <el-select v-model="form.collegeId" placeholder="请选择院系" style="width:150px;" clearable>
          <el-option v-for="item in CollegeName" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="房间号" style="font-weight:bold;">
        <el-input v-model="form.name" style="width:150px" placeholder="请输入房间号"></el-input>
      </el-form-item> -->
      <el-form-item label="是否购买卧具包" style="font-weight:bold;" label-width="110px">
        <el-select v-model="form.state" placeholder="请选择" style="width:150px">
          <el-option label="是" value="1"></el-option>
          <el-option label="否" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-button size="small" class="search_button" type="primary" icon="el-icon-search" @click="getForm">查询</el-button>
      <el-button size="small" class="reset_button" icon="el-icon-refresh" @click="reset">重置</el-button>
    </el-form>

    <el-button size="small" type="success" plain icon="el-icon-download" @click="exportGraduate">导出新生入住信息</el-button>
    <el-divider></el-divider>


    <el-table :data="tableData" border size="medium">
      <!-- <el-table-column prop="campusName" label="校区" align="center">
      </el-table-column> -->
      <el-table-column prop="userName" label="姓名" align="center" min-width="90">
      </el-table-column>
      <el-table-column prop="userNum" label="学号" align="center" min-width="100">
      </el-table-column>
      <el-table-column prop="sex" label="性别" align="center" width="70">
        <template slot-scope="scope">
          <span v-if="scope.row.sex == 1">男</span>
          <span v-if="scope.row.sex == 0">女</span>
        </template>
      </el-table-column>
      <el-table-column prop="grade" label="年级" align="center" width="80">
      </el-table-column>
      <el-table-column prop="scholasticYear" label="学年制" align="center" width="80">
      </el-table-column>
      <el-table-column prop="type" label="学生类型" align="center" width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.type == 0">本科生</span>
          <span v-if="scope.row.type == 1">硕士生</span>
          <span v-if="scope.row.type == 2">博士生</span>
          <span v-if="scope.row.type == 3">留学生</span>
          <span v-if="scope.row.type == 4">交换生</span>
        </template>
      </el-table-column>
      <el-table-column prop="collegeName" label="院系" align="center" min-width="110">
      </el-table-column>
      <el-table-column prop="userPhone" label="手机号" align="center" width="110">
      </el-table-column>



      <!-- <el-table-column prop="" label="楼宇" align="center">
      </el-table-column>
      <el-table-column prop="" label="房间号" align="center">
      </el-table-column>
      <el-table-column prop="" label="床位号" align="center">
      </el-table-column>
      <el-table-column prop="" label="床位状态" align="center">
      </el-table-column>
      <el-table-column prop="" label="可住人数" align="center">
      </el-table-column>
      <el-table-column prop="" label="入住时间" align="center" width="100">
      </el-table-column>
      <el-table-column prop="" label="退宿时间" align="center">
      </el-table-column> -->
      <el-table-column prop="state" label="是否购买卧具包" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 1">购买</span>
          <span v-if="scope.row.state == 0">不购买</span>
        </template>
      </el-table-column>
      <el-table-column prop="submitTime" label="提交时间" align="center" width="100">
      </el-table-column>
      <el-table-column prop="notes" label="备注" align="center">
      </el-table-column>
    </el-table>
    <el-pagination class="pagination" background layout="total, sizes, prev, pager, next, jumper" :total="total"
      :page-size="10" :page-sizes="[10, 20, 30, 40]" @current-change="handleCurrentChange"
      @size-change="handleSizeChange"></el-pagination>
  </div>
</template>

<script>
import { getCampusName, getBuildingName } from '_api/DormManage/Building/index.js'
import { getCollegeName } from '_api/DormManage/College/index.js'
import { getForm, exportGraduate, } from '_api/DormManage/GraduateManage/index.js'

export default {
  data() {
    return {

      form: {
        current: 0,
        pageSize: 10,
        buildId: "",
        campusId: "",
        collegeId: "",
        userName: "",
        userNum: "",
        grade: "",
        type: "",
        state: ""
      },
      tableData: [],
      total: 0,
      CampusList: [],
      buildName: [],
      CollegeName: [],
      timeNow: ""
    }
  },
  watch: {
    form: {
      // 执行方法
      handler(oldValue, newValue) {
        getBuildingName({ campusId: newValue.campusId }).then(res => {
          if (res.status == 200) {
            this.buildName = res.data.data
          }
        })
      },
      deep: true, // 深度监听
      // immediate: true  // 第一次改变就执行
    }
  },
  created() {
    this.getForm()
  },
  methods: {
    changeSwitch(event) {
      console.log(event);
    },
    //获取当前时间
    getCurrentTime() {
      var _this = this;
      let yy = new Date().getFullYear();
      let mm = new Date().getMonth() + 1;
      let dd = new Date().getDate();
      let hh = new Date().getHours();
      let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
      let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
      this.timeNow = yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf + ':' + ss;

    },
    // 导出研究生入住信息
    exportGraduate() {
      exportGraduate(this.form).then(res => {
        const link = document.createElement('a')  //创建a标签
        const blob = new Blob([res.data], { type: 'application/vnd.ms-excel' }) //设置文件流
        link.style.display = 'none'// 设置连接
        link.href = URL.createObjectURL(blob)  //将文件流转化为blob地址
        this.getCurrentTime()
        link.download = '研究生入住信息信息报表_' + this.timeNow
        document.body.appendChild(link) // 模拟点击事件
        link.click()  //设置点击事件
        this.$message.success('导出成功！')
      })
    },
    //重置
    reset() {
      this.form = {
        current: 0,
        pageSize: 10,
        buildId: "",
        campusId: "",
        collegeId: "",
        userName: "",
        userNum: "",
        grade: "",
        type: "",
        state: ""

      }
      this.getForm()
    },
    //当前页
    handleCurrentChange(e) {
      this.form.current = (e - 1) * 10
      this.getForm()
    },
    handleSizeChange(e) {
      this.form.pageSize = e
      this.getForm()
    },
    //查询校区名称
    getCampusName() {
      getCampusName().then(res => {
        if (res.status == 200) {
          this.CampusList = res.data.data
          this.getBuildingName()
        }
      })
    },
    //获取楼宇名称
    getBuildingName() {
      getBuildingName({ type: 0 }).then(res => {
        if (res.status == 200) {
          this.buildName = res.data.data
          this.getCollegeName()
        }
      })
    },
    //获取学院名称
    getCollegeName() {
      getCollegeName().then(res => {
        if (res.status == 200) {
          this.CollegeName = res.data.data
        }
      })
    },
    //查询列表信息
    getForm() {
      getForm(this.form).then(res => {
        if (res.data.success == true) {
          this.tableData = res.data.data.list
          this.total = res.data.data.total
          this.getCampusName()
        } else {
          this.tableData = res.data.data
          this.total = ""
          this.$message.info(res.data.message)
        }
      })
    },

  }
}
</script>

<style lang="scss" scoped>
.pagination {
  float: right;
  margin-top: 10px;
}
</style>